import Vue from 'vue'
import Router from 'vue-router'

import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'

Vue.use(Router)

import Layout from 'views/Layout/index.vue'

const routes = [
  { path: '/index', redirect: '/', hidden: true },
  {
    path: '/',
    component: Layout,
    redirect: '/list',
    children: [
      {
        path: '/list',
        name: 'list',
        meta: { title: '首页', icon: 'icon-shouye1' },
        component: () => import('views/Article/index')
      },
      {
        path: '/article/:id',
        name: 'article',
        hidden: true,
        meta: { title: '文章', icon: 'table' },
        component: () => import('views/Article/article')
      },
      {
        path: '/archive',
        name: 'archive',
        meta: { title: '归档', icon: 'icon-yiguidang' },
        component: () => import('views/Archive')
      },
      {
        path: '/sorts',
        name: 'sorts',
        meta: { title: '分类', icon: 'icon-icon' },
        component: () => import('views/Sorts')
      },
      {
        path: '/tags',
        name: 'tags',
        meta: { title: '标签', icon: 'icon-mark' },
        component: () => import('views/Tags')
      },
      {
        path: '/music',
        name: 'music',
        meta: { title: '音乐', icon: 'icon-yinle' },
        component: () => import('views/Music')
      },
      {
        path: '/link',
        name: 'link',
        meta: { title: '友链', icon: 'icon-youlian' },
        component: () => import('views/Link')
      },
      {
        path: '/about',
        name: 'about',
        meta: { title: '关于', icon: 'icon-guanyu3' },
        component: () => import('views/About')
      },
      {
        path: '/search',
        name: 'search',
        hidden: true,
        meta: { title: 'search', icon: 'table' },
        component: () => import('views/Search')
      }
    ]
  },
  { path: '/404', name: '404', hidden: true, component: () => import('views/404') },
  { path: '*', redirect: '/404', hidden: true }
]

const scrollBehavior = (to, from, savedPosition) => {
  if (savedPosition) {
    return savedPosition
  } else {
    const position = {}
    if (to.hash) {
      position.selector = to.hash
    }
    if (to.matched.some(m => m.meta.scrollToTop)) {
      position.x = 0
      position.y = 0
    }
    return position
  }
}

const router = new Router({
  // mode: 'history', // 后端支持可开
  // base: '/www/blog/client/',
  routes,
  scrollBehavior
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

router.afterEach((to, from) => {
  document.title = to.meta.title + ' | 于梦中的博客空间'
  NProgress.done() // 结束Progress
})

export default router
